﻿namespace com.stifel.ServerBuildMgmtDao
{
    using System.Linq;

    using com.stifel.SharePoint;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    using Context = StifelServerBuildDataContext;

    using CustomObject = StifelVirtualServerRequestConfiguration;

    public class StifelVirtualServerRequestConfigurationDao : BaseDao
    {
        #region Fields

        const string ListName = "stifelvirtualserverrequestconfiguration";

        #endregion Fields

        #region Constructors

        public StifelVirtualServerRequestConfigurationDao( )
            : base(ListName)
        {
            // set to the site collection url
            CurrentWebUrl = SPContext.Current.Site.RootWeb.Url;
        }

        public StifelVirtualServerRequestConfigurationDao( string currentWebUrl )
            : base(ListName)
        {
            // set to the site collection url
            using ( SPSite spSite = new SPSite( currentWebUrl ) )
            {
                using ( SPWeb spWeb = spSite.RootWeb )
                {
                    CurrentWebUrl = spWeb.Url;
                }
            }
        }

        #endregion Constructors

        #region Methods

        public void Add( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                l.InsertOnSubmit( clean( value ) );
                c.SubmitChanges( );
            }
        }

        public void Clear( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                var v = ( from o in l
                          where o.Id > 0
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public CustomObject Get( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                              where o.Active == true
                                                              select o ).FirstOrDefault( );
                if ( v == null )
                    v = new CustomObject( );
                return v;
            }
        }

        CustomObject clean( CustomObject value )
        {
            return new CustomObject
            {
                Title = value.Title,
                Active = value.Active,
                ADNotificationTitle = value.ADNotificationTitle,
                ADNotificationTemplate = value.ADNotificationTemplate,
                ADPageURL = value.ADPageURL,
                ADSecurityGroupId = value.ADSecurityGroupId,
                ADSendEmail = value.ADSendEmail,
                ADSiteURL = value.ADSiteURL,
                AVNotificationTitle = value.AVNotificationTitle,
                AVNotificationTemplate = value.AVNotificationTemplate,
                AVPageURL = value.AVPageURL,
                AVSecurityGroupId = value.AVSecurityGroupId,
                AVSendEmail = value.AVSendEmail,
                AVSiteURL = value.AVSiteURL,
                ApprovalNotificationTitle = value.ApprovalNotificationTitle,
                ApprovalNotificationTemplate = value.ApprovalNotificationTemplate,
                ApprovalPageURL = value.ApprovalPageURL,
                ApprovalSecurityGroupId = value.ApprovalSecurityGroupId,
                ApprovalSendEmail = value.ApprovalSendEmail,
                ApprovalSiteURL = value.ApprovalSiteURL,
                BackupNotificationTitle = value.BackupNotificationTitle,
                BackupNotificationTemplate = value.BackupNotificationTemplate,
                BackupPageURL = value.BackupPageURL,
                BackupSecurityGroupId = value.BackupSecurityGroupId,
                BackupSendEmail = value.BackupSendEmail,
                BackupSiteURL = value.BackupSiteURL,
                BuildNotificationTitle = value.BuildNotificationTitle,
                BuildNotificationTemplate = value.BuildNotificationTemplate,
                BuildPageURL = value.BuildPageURL,
                BuildSecurityGroupId = value.BuildSecurityGroupId,
                BuildSendEmail = value.BuildSendEmail,
                BuildSiteURL = value.BuildSiteURL,
                MonitorNotificationTitle = value.MonitorNotificationTitle,
                MonitorNotificationTemplate = value.MonitorNotificationTemplate,
                MonitorPageURL = value.MonitorPageURL,
                MonitorSecurityGroupId = value.MonitorSecurityGroupId,
                MonitorSendEmail = value.MonitorSendEmail,
                MonitorSiteURL = value.MonitorSiteURL,
                PatchNotificationTitle = value.PatchNotificationTitle,
                PatchNotificationTemplate = value.PatchNotificationTemplate,
                PatchPageURL = value.PatchPageURL,
                PatchSecurityGroupId = value.PatchSecurityGroupId,
                PatchSendEmail = value.PatchSendEmail,
                PatchSiteURL = value.PatchSiteURL,
                RequestAcceptedNotificationTemplate = value.RequestAcceptedNotificationTemplate,
                RequestAcceptedNotificationTitle = value.RequestAcceptedNotificationTitle,
                RequestCompletedNotificationTemplate = value.RequestCompletedNotificationTemplate,
                RequestCompletionNotificationTitle = value.RequestCompletionNotificationTitle,
                RequestNotificationTemplate = value.RequestNotificationTemplate,
                RequestNotificationTitle = value.RequestNotificationTitle,
                RequestorPageURL = value.RequestorPageURL,
                RequestRejectedNotificationTemplate = value.RequestRejectedNotificationTemplate,
                RequestRejectedNotificationTitle = value.RequestRejectedNotificationTitle,
                RequestSiteURL = value.RequestSiteURL
            };
        }

        #endregion Methods
    }
}